﻿using BaseDal;
using Common;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseBll
{
    public class ItemBll
    {
        private static readonly Lazy<ItemBll> bll = new Lazy<ItemBll>(() => new ItemBll());
        public static ItemBll Instance { get; } = bll.Value;

        private ItemBll()
        { }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return ItemDal.Instance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return ItemDal.Instance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Item> GetList(string strWhere)
        {
            return ItemDal.Instance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Item GetModelById(int id)
        {
            return ItemDal.Instance.GetModelById(id);
        }

        /// <summary>
        /// 新增物品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddItem(Item model)
        {
            return ItemDal.Instance.AddItem(model);
        }

        /// <summary>
        /// 修改物品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateItem(Item model)
        {
            return ItemDal.Instance.UpdateItem(model);
        }

        /// <summary>
        /// 删除物品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteItem(string ids)
        {
            return ItemDal.Instance.DeleteItem(ids);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(Item model, ref string msg)
        {
            if (string.IsNullOrEmpty(model.ItemName))
            {
                msg = "物品名称不能为空";
                return false;
            }
            if (string.IsNullOrEmpty(model.Specs))
            {
                msg = "规格型号不能为空";
                return false;
            }

            return true;
        }

        #region --物品类别相关--

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExistType(string strWhere)
        {
            return ItemDal.Instance.IsExistType(strWhere);
        }

        /// <summary>
        /// 查询类别list
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<ItemType> GetListTypeModels(string strWhere)
        {
            return ItemDal.Instance.GetListTypeModels(strWhere);
        }

        /// <summary>
        /// 将list转为树形json结构
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public JArray GetTreeJson(List<ItemType> list)
        {
            JArray result = new JArray();
            List<ItemType> parentList = GetParentNodes(list);
            JArray children;
            JObject parent;

            foreach (ItemType model in parentList)
            {
                children = GetChilds(list, model.ID);
                parent = new JObject
                {
                    ["id"] = model.ID,
                    ["key"] = Utils.GenerateUniqueRandomString(6),
                    ["title"] = model.TypeName
                };
                if (children.Count > 0)
                {
                    parent["hasChildren"] = true;
                    parent["children"] = children;
                }
                result.Add(parent);
            }
            return result;
        }

        /// <summary>
        /// 获取父节点信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<ItemType> GetParentNodes(List<ItemType> list)
        {
            List<ItemType> result = new List<ItemType>();
            result.AddRange(list.FindAll(a => a.ParentId == 0));
            return result;
        }

        /// <summary>
        /// 子节点数据
        /// </summary>
        /// <param name="list">列表数据</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        public JArray GetChilds(List<ItemType> list, int parentId)
        {
            JArray result = new JArray();
            foreach (ItemType model in list)
            {
                if (model.ParentId == parentId)
                {
                    JObject obj = new JObject();
                    JArray children = GetChilds(list, model.ID);
                    obj["id"] = model.ID;
                    obj["key"] = Utils.GenerateUniqueRandomString(6);
                    obj["title"] = model.TypeName;
                    if (children.Count > 0)
                    {
                        obj["hasChildren"] = true;
                        obj["children"] = children;
                    }
                    result.Add(obj);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取某一分类下的所有子分类
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public List<ItemType> GetTypeByName(string typeName)
        {
            return ItemDal.Instance.GetTypeByName(typeName);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ItemType GetTypeModelById(int id)
        {
            return ItemDal.Instance.GetTypeModelById(id);
        }

        /// <summary>
        /// 新增物品类别
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddItemType(ItemType model)
        {
            return ItemDal.Instance.AddItemType(model);
        }

        /// <summary>
        /// 删除物品类别
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool DeleteItemType(int id)
        {
            return ItemDal.Instance.DeleteItemType(id);
        }

        /// <summary>
        /// 查询物品表中是否使用该类别
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public bool HasUsed(int typeId)
        {
            return IsExist(" and TypeID=" + typeId);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel_Type(ItemType model, ref string msg)
        {
            if (string.IsNullOrEmpty(model.TypeName))
            {
                msg = "类别名称不能为空";
                return false;
            }
            string strWhere = "";
            if (model.ID > 0)
            {
                strWhere = "TypeName='" + model.TypeName + "' and ID !=" + model.ID;
            }
            else
            {
                strWhere = "TypeName='" + model.TypeName + "'";
            }
            if (IsExistType(strWhere))
            {
                msg = "类别名称已存在";
                return false;
            }
            if (model.ParentId < 0)
            {
                model.ParentId = 0;
            }

            return true;
        }

        #endregion --物品类别相关--
    }
}